/**
 *
 */
/**
 * @author Administrator
 *
 */
package pdf;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.google.zxing.WriterException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;


class TestMakePdf{
	public static void main(String[] args) throws Exception {
		String content="身份证号:412725199006148677\n手机号：17999999999";
		//"C:/a.jpg";
		String logUri="";
		int[]  size=new int[]{430,430};
		String format = "jpg";
		BufferedImage bufferedImage=null;
		try {
			bufferedImage=new  QRCodeFactory().CreatQrImage1(content, format, "", logUri,size);
			//BufferedImage bufferedImage=new  QRCodeFactory().CreatQrImage1(content, format, imageTempPath, logUri,size);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriterException e) {
			e.printStackTrace();
		}


		// 模板文件路径
		String templatePath = "D:/ceshi1.pdf";
		// 生成的文件路径
		String targetPath = "D:/ceshi2.pdf";
		// 读取模板文件
		InputStream input = new FileInputStream(new File(templatePath));
		PdfReader reader = new PdfReader(input);
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetPath));
		//这里是写死要插入的坐标
		float x = 300;
		float y = 500;

		// 读图片
		//Image image = Image.getInstance(imageTempPath);
		Image image=getImage(bufferedImage,99);

		// 获取操作的页面
		PdfContentByte under = stamper.getOverContent(1);
		// 根据域的大小缩放图片
		image.scaleToFit(300, 300);
		// 添加图片
		image.setAbsolutePosition(x, y);
		under.addImage(image);

		stamper.close();
		reader.close();
		System.out.println("操作完成");

	}
	public static Image getImage(String imgPath,float percent) throws Exception{
		java.awt.Image image=null;
		Image itextImage=null;
		image=ImageIO.read(new File(imgPath));
		itextImage=Image.getInstance(image,new Color(255,255,255));
		itextImage.setWidthPercentage(0.1f);
		itextImage.setSpacingAfter(0.1f);
		itextImage.scalePercent(percent);
		itextImage.setBorder(Rectangle.NO_BORDER);
		return itextImage;
	}

	public static Image getImage(BufferedImage bufferedImage,float percent) throws Exception{
		Image itextImage=null;
		itextImage=Image.getInstance(bufferedImage,new Color(255,255,255));
		itextImage.setWidthPercentage(0.1f);
		itextImage.setSpacingAfter(0.1f);
		itextImage.scalePercent(percent);
		itextImage.setBorder(Rectangle.NO_BORDER);
		return itextImage;
	}
}